<?php

defined('SYSPATH') or die('Access error!');

class Controller_Admin_Ads extends Controller_Admin_Component
{

    private $ads;

    public function before()
    {
        parent::before();
        $this->ads = Model_Ads::factory('ads');
        I18n::set_current_file('ads');
    }

    public function action_add()
    {
        if ($_POST)
        {
            $_POST['add_time'] = Date::get_time();
            $this->save($_POST);
        }
        $this->template->assign('ads_category_list', $this->get_category_cache_option('ads'));
        $this->template->view('component_ads_add');
    }

    public function action_edit()
    {
        $id = intval($this->request->param('id'));
        $ads_detail = $this->ads->get_ads_detail($id);
        if ($_POST)
        {
            $this->save($_POST, array('id' => $id));
        }

        $this->template->assign('ads_category_list', $this->get_category_cache_option('ads', $ads_detail['category_id']));
        $this->template->assign('data', $ads_detail);
        $this->template->view('component_ads_edit');
    }

    public function action_list()
    {
        //获取的广告列表
        $this->display_list($this->ads->get_ads_list());
    }

    public function action_search()
    {
        $ads_category = Arr::get($_POST, 'ads_category', 0);
        $keyword = Arr::get($_POST, 'keyword', '');
        $data = array();
        if ($ads_category != 0)
        {
            $data['category_id'] = $ads_category;
        }
        if (!empty($keyword))
        {
            $data['name'] = $keyword;
        }

        //获取的广告列表
        $ads_list = $this->ads->get_ads_list($data);
        $this->display_list($ads_list);
    }

    public function display_list($ads_list)
    {
        //缓存的广告分类--用于搜索
        $this->template->assign('category_option', $this->get_category_cache_option('ads'));
        $this->template->assign('ads_list', $ads_list['list']);
        $this->template->assign('page', $ads_list['page']);
        $this->template->view('component_ads_list');
    }

    public function action_batch_position()
    {
        $position_arr = $_POST['position'];
        if (count($position_arr) > 0)
        {
            foreach ($position_arr as $key => $value)
            {
                $this->ads->update(array('position' => trim($value)), array('id' => $key));
            }
        }
        Message::set(Message::SUCCESS, '排序成功');
        $this->request->redirect('admin/ads/list');
    }

    public function action_delete()
    {
        $id = $this->request->param('id');
        if ($this->ads->delete(array('id' => $id)))
        {
            Message::success(__('delete success'));
            $this->request->redirect(Route::get('admin/ads')->uri(array('action' => 'list')));
        }
    }

    public function handle_post($data)
    {
        $data['position'] = Arr::get($data, 'position', 9999);
        $data['click'] = Arr::get($data, 'click', '');
        $data['click'] = !empty($data['click']) ? $data['click'] : Text::random('nozero', 2);
        $data['update_time'] = Date::get_time();
        unset($data['dosubmit']);
        return parent::handle_post($data);
    }

    public function save($post, $where_arr = array())
    {
        $data = $this->handle_post($post);
        if (!$this->ads->validate($data))
        {
            Message::error(__('validate error'));
            $this->request->redirect(Route::get('admin/ads')->uri(array('action' => 'edit', 'id' => $where_arr['id'])));
        }
        if ($_FILES['file_name']['tmp_name'] != '')
        {
            $time_path = File::create_time_path();
            $data['file_name'] = $this->save_simple_file(COMPONENT_ADS_SAVE_PATH . $time_path, 'file_name');
            $data['file_path'] = File::change_path($time_path);
        }
        if (Arr::validate_arr($where_arr))
        {
            if ($this->ads->update($data, array('id' => $where_arr['id'])))
            {
                Message::success(__('update success'));
                $this->request->redirect(Route::get('admin/ads')->uri(array('action' => 'edit', 'id' => $where_arr['id'])));
            }
        }
        else
        {
            if ($this->ads->insert($data))
            {
                Message::set(Message::SUCCESS, "\"" . $data['name'] . "\"" . '--广告添加成功');
                $this->request->redirect(Route::get('admin/ads')->uri(array('action' => 'add')));
            }
        }
    }

}

